package org.zhadoop.stream.cql.semanticanalyzer;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.stream.api.streams.Column;
import org.zhadoop.stream.api.streams.Schema;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.semanticanalyzer.analyzecontext.AnalyzeContext;
import org.zhadoop.stream.cql.semanticanalyzer.analyzecontext.SelectClauseAnalyzeContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.AtomExpressionContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.BaseExpressionParseContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.FieldExpressionContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.ParseContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.SelectClauseContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.SelectItemContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.StreamAllColumnsContext;

import com.google.common.collect.Lists;

/**
 * select 子句语法分析
 * @author zwd
 *
 */
public class SelectClauseAnalyzer extends BaseAnalyzer {

	private static final Logger LOG = LoggerFactory.getLogger(SelectStatementAnalyzer.class);
	
	private static final String DEFAULT_SCHEMA_NAME = "tmpschema";
	
	private SelectClauseAnalyzeContext selectAnalyzeContext;
	
	private SelectClauseContext selectClauseParseConext;
	
	private List<BaseExpressionParseContext> selectExpressions = null;
	
	private Schema selectOutputSchema;
	
	
	public SelectClauseAnalyzer(ParseContext parseContext) throws SemanticAnalyzerException {
		super(parseContext);
		selectClauseParseConext = (SelectClauseContext) parseContext;
		selectExpressions = Lists.newArrayList();
		selectOutputSchema = new Schema(DEFAULT_SCHEMA_NAME);
	}

	@Override
	public AnalyzeContext analyze() throws SemanticAnalyzerException {
		parseSelectItems();
		createSelectExpressionDescs();
		return null;
	}

	
	private void createSelectExpressionDescs() throws SemanticAnalyzerException{
		for(BaseExpressionParseContext exps : selectExpressions){
		}
	}

	private void parseSelectItems() throws SemanticAnalyzerException{
		selectAnalyzeContext.setDistinct(selectClauseParseConext.isDistinct());
		for(SelectItemContext selectItem : selectClauseParseConext.getSelectItems()){
			parseSelectItem(selectItem);
		}
	}

	private void parseSelectItem(SelectItemContext selectItem) throws SemanticAnalyzerException{
		boolean selectStar = selectItem.getExpression().getAllColumns() != null;
		if(selectStar){
			parseStarExpression(selectItem);
		}
		
	}

	private void parseStarExpression(SelectItemContext selectItem) throws SemanticAnalyzerException{
		if(isStarWithStreamNameExpression(selectItem.getExpression().getAllColumns())){
			
		}else{
			parseStarWithOutStreamNameExpression();
		}
	}

	private void parseStarWithOutStreamNameExpression() {
		for(Schema schema : getAllSchemas()){
			for(Column column : schema.getCols()){
				String colName = column.getName();
				if(selectOutputSchema.isAttributeExist(colName)){
					colName = renameNewName(selectOutputSchema, colName);
				}
				selectOutputSchema.addCol(new Column(colName, null));
				selectExpressions.add(createFieldExpression(schema.getId(), column.getName()));
			}
		}
	}

	private BaseExpressionParseContext createFieldExpression(String schemaName, String columnName) {
		AtomExpressionContext atmoExp = craeteAtomExpression(columnName);
		FieldExpressionContext fexp = new FieldExpressionContext();
		fexp.setStreamNameOrAlias(schemaName);
		fexp.setAtomExpression(atmoExp);
		return fexp;
	}

	private AtomExpressionContext craeteAtomExpression(String columnName) {
		AtomExpressionContext atomExp = new AtomExpressionContext();
		atomExp.setColumnName(columnName);
		return atomExp;
	}

	/**
	 * 是否包含了Schema名称
	 * @param allColumns
	 * @return
	 */
	private boolean isStarWithStreamNameExpression(StreamAllColumnsContext allColumns) {
		return allColumns.getStreamName() != null;
	}

	@Override
	protected AnalyzeContext getAnalyzeContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void createAnalyzeContext() {
		// TODO Auto-generated method stub
		
	}

}
